package apiUtils.apis;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import apiUtils.APISHelper;
import apiUtils.CallBack;
import apiUtils.params.AESParams;
import apiUtils.params.Params;
import appUtils.LogUtil;
import io.reactivex.functions.Consumer;
import okhttp3.Call;

import static constant.ApiConstants.TASK_SUCCESS;

/**
 * Created by Administrator on 2017/9/1.
 */

public class SwitchChannelAPI extends APISHelper {
    private static final String URL = "/services/getServerPayList/testThree";
    private static final String TAG = "GetPayTypesAPI";
    private String mProductTypeCode;
    private String mChannelCode;
    private String mTypeCode;
    private String mAppid;

    public void switchChannel(String productTypeCode, String channelCode, String typeCode, String appid, final Listener listener) {
        mProductTypeCode = productTypeCode;
        mChannelCode = channelCode;
        mTypeCode = typeCode;
        mAppid = appid;
        try {
            get(TAG, URL, new CallBack() {
                @Override
                public void onSuccess(Call call, String response) throws IOException {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if (TASK_SUCCESS.equals(jsonObject.getString("flag"))) {
                            String message = jsonObject.getString("msg");
                            LogUtil.log("GetPayTypesAPI message = " + message);
                            success(message, listener);
                        } else {
                            failure(-1, "", listener);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        failure(-1, e.getMessage(), listener);
                    }
                }

                @Override
                public void onError(int code) {
                    failure(code, "", listener);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
            failure(-1, e.getMessage(), listener);
        }
    }

    @Override
    protected Params initParams() throws JSONException {
        AESParams params = new AESParams();
        params.put("productTypeCode", mProductTypeCode);
        params.put("channelCode", mChannelCode);
        params.put("typeCode", mTypeCode);
        params.setAppid(mAppid);
        return params;
    }

    private void success(final String message, final Listener listener) {
        subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                if (null != listener) {
                    listener.onSuccess(message);
                }
            }
        });
    }

    private void failure(final int code, final String msg, final Listener listener) {
        subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                if (listener != null) {
                    listener.onFailure(code, msg);
                }
            }
        });
    }

    public interface Listener {
        void onSuccess(String message);

        void onFailure(int code, String message);
    }
}
